<form class="layui-form seller-form"  action="" >
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label seller-inline-2">任务名称：</label>
            <div class="layui-input-inline">
                <input type="text" name="name" lay-verify="title" placeholder="请输入任务名称" autocomplete="off" class="layui-input">
            </div>
        </div>
        <!-- <div class="layui-inline">
            <label class="layui-form-label">任务状态：</label>
            <div class="layui-input-inline">
                <select name="status" id="status">
                    <option value="">全部</option>
                    <option value="0">等待执行</option>
                    <option value="1">正在导出</option>
                    <option value="2">导出成功</option>
                    <option value="3">导出失败</option>
                    <option value="4">正在导入</option>
                    <option value="5">导入成功</option>
                    <option value="6">导入失败</option>
                    <option value="7">导入中断</option>
                    <option value="8">部分导入</option>
                </select>
            </div>
        </div> -->

        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm" lay-submit lay-filter="task-search"><i class="iconfont icon-chaxun"></i>筛选</button>
        </div>
    </div>
</form>


<div class="table-body">
    <table id="taskTable" lay-filter="taskTable"></table>
</div>

<script> 
    layui.use(['table','form','layer','laydate'],function(){
        var layer = layui.layer;
        var table = layui.table;
        // 定义变量保存当前页面和当前页面数据条数
        var currPage; 
        var dataNum;

        //执行渲染
        var taskTable = layui.table.render({
            elem: '#taskTable', //指定原始表格元素选择器（推荐id选择器）
            height: 'full-115',
            cellMinWidth: '80',
            page: 'true',
            limit:'20',
            id:'taskTable',
            url: "{:url('ietask/index')}",
            cols: [[
                {type:'numbers'},
                {field: 'name', title: '任务名称',align:'center', width: 160},
                {field: 'type', title: '任务类型',align:'center', width: 100},
                {field: 'file_name', title: '文件名称',align:'center', width: 350},
                {field: 'count', title: '数据总数',align:'center', width: 100},
                //{field: 'progress', title: '当前进度',align:'center', width: 100},
                //{field: 'err_count', title: '错误数据',align:'center', width: 100}, 
                {field: 'file_path', title: '文件下载',align:'center', width: 100,templet:function (data) {
                    return '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="downfile('+data.id+')">下载文件</button>';
                }},
                // {field: 'file_path', title: '错误文件下载',align:'center', width: 120,templet:function (data) {
                //     if(data.err_count && data.err_count > 0) {
                //         return '<button class="layui-btn layui-btn-sm layui-btn-normal" onclick="downerrfile('+data.id+')">下载文件</button>';
                //     } else {
                //         return "";
                //     }
                // }},
                {field: 'ctime',sort: true, title: '创建时间' ,align:'center', width: 200},
                {field: 'operating', title: '操作', width:100, align: 'center', width: 100,templet:function(data){
                        var html = '';
                        html += '<a  class="layui-btn layui-btn-xs task-del layui-btn-danger" data-id="' + data.id + '">删除</a>';
                        return html;
                    }},
            ]], //设置表头
            done: function(res, curr, count){
                // 给变量赋值 
                dataNum = res.data.length;
                currPage = curr;
            }
        });
        //查看操作
        $(document).on('click','.task-del',function(){
            var id = $(this).attr('data-id');
            //删除
            layer.confirm('真的删除么', {icon: 3}, function(index){
                JsGet("{:url('manage/ietask/del')}?id="+id, function(res){
                    //删除逻辑中判断只有一条数据时应该刷新前一页信息
                    layer.msg(res.msg);
                    if(res.status){
                        if(dataNum == 1){
                            currPage = currPage - 1;
                        }

                        table.reload('taskTable', {
                            page: {
                                curr: currPage
                            }
                        });
                        return false;
                    }
                });
            });
        });

        layui.form.on('submit(task-search)', function(data){
            layui.table.reload('taskTable', {
                where: data.field
                , page: {
                    curr: 1 //重新从第 1 页开始
                }
            }); 
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
    //文件下载
    function downfile(id) {
        JsGet("{:url('ietask/down')}?id="+id,function (res) {

            if(res.status){
                layer.msg("开始下载");
                window.location.href=res.data.url;
            }else{
                layer.msg(res.msg);
            }

        });
    }//错误文件下载
    function downerrfile(id) {
        JsGet("{:url('ietask/downerr')}?id="+id,function (res) {

            if(res.status){
                layer.msg("开始下载");
                window.location.href=res.data.url;
            }else{
                layer.msg(res.msg);
            }

        });
    }
</script>